package org.example;

import io.restassured.RestAssured;
import io.restassured.filter.FilterContext;
import io.restassured.response.Response;
import io.restassured.specification.FilterableRequestSpecification;
import io.restassured.specification.FilterableResponseSpecification;
import org.example.utils.ReadConf;


import java.util.*;


//
//
//
//import io.restassured.RestAssured;
//import io.restassured.filter.Filterable;
//import io.restassured.response.Response;
//
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
public class CookieHandlingFilter implements RequestFilter,  ResponseFilter {

    // 用于存储全局 Cookie 的 Map
    public static Map<String, String> cookies = new HashMap<>();
    private static boolean loggedIn = false;

    @Override
    public void filter(FilterableRequestSpecification requestSpecification, FilterContext filterContext) {
        if (loggedIn) {
            for (Map.Entry<String, String> entry : cookies.entrySet()) {
                requestSpecification.cookie(entry.getKey(), entry.getValue());
            }
        }
    }

    @Override
    public Response filter(FilterableRequestSpecification requestSpecification, FilterableResponseSpecification responseSpecification, FilterContext filterContext) {
        Response response = filterContext.next(requestSpecification, responseSpecification);

        List<String> setCookieHeaders = response.getHeaders().getValues("Set-Cookie");
        if (setCookieHeaders != null) {
            for (String header : setCookieHeaders) {
                String[] cookieParts = header.split(";");
                String cookieName = null;
                String cookieValue = null;

                for (String part : cookieParts) {
                    part = part.trim();
                    if (part.contains("=")) {
                        String[] nameValue = part.split("=", 2);
                        cookieName = nameValue[0];
                        cookieValue = nameValue.length > 1 ? nameValue[1] : "";
                    }
                }

                if (cookieName != null) {
                    cookies.put(cookieName, cookieValue);
                }
            }
        }

        if (!loggedIn || cookiesExpired()) {
            reLogin();
        }

        return response;
    }

    private boolean cookiesExpired() {
        // 实现 Cookie 过期判断逻辑
        return !loggedIn; // 示例
    }

    private void reLogin() {
        String psd = ReadConf.readConf("userPasswordForQcdz");
        String loginName = ReadConf.readConf("userNameForQcdz");
        Map<String, String> formParams = new HashMap<>();
        formParams.put("loginName", loginName);
        formParams.put("psd", psd);

        Response response = RestAssured.given()
                .header("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8")
                .formParams(formParams)
                .baseUri("http://10.1.10.141:9031/")
                .post("login.do?method=login_qyzh");

        if (response.getStatusCode() == 302) { // 302 是常见的重定向状态码
            String zyOpUser = response.getCookie("zyOpUser");

            if (zyOpUser != null) {
                Response loginResponse = RestAssured.given()
                        .cookie("zyOpUser", zyOpUser)
                        .get("http://10.1.10.141:9031/unite.do?method=enterGrzx&csrftoken=");

                if (loginResponse.getStatusCode() == 200) {
                    loggedIn = true;

                    List<String> setCookieHeaders = loginResponse.getHeaders().getValues("Set-Cookie");
                    if (setCookieHeaders != null) {
                        for (String header : setCookieHeaders) {
                            String[] cookieParts = header.split(";");
                            String cookieName = null;
                            String cookieValue = null;

                            for (String part : cookieParts) {
                                part = part.trim();
                                if (part.contains("=")) {
                                    String[] nameValue = part.split("=", 2);
                                    cookieName = nameValue[0];
                                    cookieValue = nameValue.length > 1 ? nameValue[1] : "";
                                }
                            }

                            if (cookieName != null && cookieValue != null) {
                                cookies.put(cookieName, cookieValue);
                            }
                        }
                    }
                }
            }
        }
    }


}





